﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace EquipmentPanelTest.Controls
{
    /// <summary>
    /// Interaction logic for EquipmentItemControl.xaml
    /// </summary>
    public partial class EquipmentItemControl : UserControl, IEquipmentItem
    {
        #region Events

        public MouseButtonEventHandler PressedEvent;
        public MouseButtonEventHandler UnPressedEvent;

        #endregion

        public EquipmentItemControl(int rowsCount, int columnsCount)
        {
            InitializeComponent();

            SetIntersected(false);
            SetSelected(false);
            RowsCount = rowsCount;
            ColumnsCount = columnsCount;
            
            MouseLeftButtonDown += new MouseButtonEventHandler(EquipmentItemControl_MouseLeftButtonDown);
            MouseLeftButtonUp += new MouseButtonEventHandler(EquipmentItemControl_MouseLeftButtonUp);
        }

        #region IEquipmentItem Members

        public bool IsSelected { get; private set; }

        public bool IsIntersected { get; private set; }

        public int RowsCount { get; private set; }

        public int ColumnsCount { get; private set; }

        public void SetIntersected(bool value)
        {
            IsIntersected = value;
            if (value)
            {
                Background = new SolidColorBrush(Colors.Red);
            }
            else
            {
                Background = new SolidColorBrush(Colors.Gold);
            }
        }

        public void SetSelected(bool value)
        {
            IsSelected = value;
            if (value)
            {
                BorderBrush = new SolidColorBrush(Colors.Green);
                BorderThickness = new Thickness(2);
            }
            else
            {
                BorderBrush = new SolidColorBrush(Colors.Gray);
                BorderThickness = new Thickness(1);
            }
        }

        #endregion

        #region Private methods

        private void EquipmentItemControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (UnPressedEvent != null)
                UnPressedEvent(sender, e);
        }

        private void EquipmentItemControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (PressedEvent != null)
                PressedEvent(sender, e);
        }

        #endregion
    }

    public interface IEquipmentItem
    {
        bool IsSelected { get; }
        bool IsIntersected { get; }
        int RowsCount { get; }
        int ColumnsCount { get; }

        void SetIntersected(bool value);
        void SetSelected(bool value);
    }
}
